package com.example.common.mybatis;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.example.common.core.constants.Constants;
import com.example.common.core.utils.ThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * 功能描述:
 *
 * @author Lenovo
 * @date 2025/3/20
 */
@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler{
    @Override
    public void insertFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject,"createTime",
                LocalDateTime.class,LocalDateTime.now());
        this.strictInsertFill(metaObject,"createBy",
                Long.class, ThreadLocalUtil.get(Constants.USER_ID, Long.class));
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        /*this.strictUpdateFill(metaObject, "createTime", LocalDateTime.class,LocalDateTime.now());
        this.strictUpdateFill(metaObject, "updateBy", Long.class, 100L);*/
        this.setFieldValByName("updateTime", LocalDateTime.now(), metaObject);
        this.setFieldValByName("updateBy",
                ThreadLocalUtil.get(Constants.USER_ID, Long.class), metaObject);
    }
}
